<template>
  <el-dialog :close-on-click-modal="false" append-to-body :title="title" :visible.sync="visible" width="800px">
    <div class="add-form">
      <el-form ref="form" :model="form" label-width="120px" :disabled="type === 'view'">
        <el-form-item label="司机" prop="account" :rules="[
          { required: true, message: '请选择司机', trigger: 'blur' }
          ]">
          <el-button :disabled="type === 'edit'" size="small" type="primary" plain @click="onSelectUser()">{{ form.account || '选择司机' }}
          </el-button>
        </el-form-item>
        <el-form-item label="仓库/中转站" prop="warehouseIds" :rules="[
          { required: true, message: '请选择仓库/中转站', trigger: 'change' }
        ]">
          <xm-select class="w-400" :options="warehouseList" v-model="form.warehouseIds" placeholder="请选择" filterable clearable multiple>
          </xm-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submit" v-show="type !== 'view'" :loading="loading">{{ loading ? '提交中...' :
          '提交'
      }}</el-button>
    </span>

    <selectUserDialog ref="selectUserDialog" @select="handleSelectUser" :customConfig="{
      request: (params) => listDriverUsers({
        ...params,
        roleNames: '小熊猫组长/小熊猫,中转司机,兼职司机'
      }),
      dataParse: (res) => {
        const resData = res.data.data || {}
        const data = {
          list: resData.rows,
          total: resData.total
        }
        return data
      },
      pageIndexKey: 'page',
    }"></selectUserDialog>
  </el-dialog>
</template>

<script>
import { bindDriverWarehouse } from '@/api/transfer'
import { getWarehouseListByCityId } from "@/api/pending";
import selectUserDialog from '@/components/dialog/selectUserDialog'
import { listDriverUsers } from '@/api/transfer'
export default {
  components: {
    selectUserDialog,
  },
  data() {
    return {
      visible: false,
      loading: false,
      type: "add",
      form: {
        "userId": "",
        "account": "",
        "warehouseIds": "",
      },
      warehouseList: []
    }
  },
  methods: {
    listDriverUsers,
    show({ type, data }) {
      this.type = type
      this.visible = true
      this.warehouseList = []
      if (data) {
        this.form = {
          ...data,
          userId: data.id,
          warehouseIds: data.warehouses ? data.warehouses.map(item => item.id + '') : []
        }
        this.getWarehouseListByCityId(data.deptId)
      } else {
        this.clearForm()
      }
    },
    clearForm() {
      this.form = {
        "userId": "",
        "account": "",
        "warehouseIds": "",
      }
      this.$refs.form && this.$refs.form.clearValidate()
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          bindDriverWarehouse(this.getParams()).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: `提交成功！`
              })
              this.visible = false
              this.$emit('submit')
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg
              })
            }
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    getParams() {
      const params = {
        "userId": this.form.userId,
        "warehouseIds": this.form.warehouseIds,
      }
      return params
    },
    getWarehouseListByCityId(cityId) {
      getWarehouseListByCityId({ cityId }).then(res => {
        this.warehouseList = (res.data.data || []).map(item => {
          return {
            ...item,
            value: item.id + '',
            label: `${item.name} - ${item.deptName}`  
          }
        })
      })
    },
    onSelectUser() {
      this.$refs.selectUserDialog.show()
    },
    handleSelectUser(user) {
      this.form.account = user.account
      this.form.userId = user.id
      this.getWarehouseListByCityId(user.deptId)
    }
  },
  computed: {
    title() {
      const textMap = {
        'add': '新 增',
        'edit': '编 辑',
        'view': '查 看',
      }
      return textMap[this.type]
    }
  },
  // created() {
  //   this.getWarehouseListByCityId()
  // }
}
</script>

<style lang="scss" scoped>
.add-form {}
</style>
